﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAO
{

	public class VUDAO:IData<VU> 
 	{
		public VUDAO(String myconnecString) : base(myconnecString) { }

		protected override int InsertOnSubmit(ref VU item, int t) {
			 SqlParameter p = new SqlParameter(); 
			 if( t == 0)
				p.Direction = ParameterDirection.Output;
			 p.ParameterName = "@" + VU.c_ID;
			 p.Value = item.ID;
			 Object obj = item.ID; 
			int i = connec.ExecuteNonQuery("sp_VU", CommandType.StoredProcedure,p,ref obj,new SqlParameter("@flag", t),
				 new SqlParameter("@" + VU.c_MAVU, item.MAVU), new SqlParameter("@" + VU.c_TENVU, item.TENVU),
				 new SqlParameter("@" + VU.c_BTVID, item.BTVID));
			if(t == 0)
				item.ID = Convert.ToInt32(obj);
			return i;
		}

		public override VU GetItem(IDataReader dr)
		{
			VU obj = new VU();
			obj.ID = Convert.ToInt32(dr[VU.c_ID]);			
			obj.TENVU = Convert.ToString(dr[VU.c_TENVU]);
			obj.BTVID = Convert.ToInt64(dr[VU.c_BTVID]);
			return obj;
		}

		public override List<VU> GetAll()
		{
			return GetAll("sp_GetAllVU", CommandType.StoredProcedure);
		}

		public override VU GetItemById(object id)
		{
			return GetAll().SingleOrDefault(p => p.ID == Convert.ToInt32(id));
		}
		public override VU GetItemById(VU item){
			throw new NotImplementedException();
		}

	}

 }
